<?php 
namespace Admin\Model;
use \Think\Model;

 class GoodsModel extends Model
 {


 	public function _before_insert(&$data,$options)
 	{
 		if(isset($_FILES['goods_img']) && $_FILES['goods_img']['error'] == 0)
		{
		
			$ret =upLoads('goods_img','Goods',array(array(160,160),array(160,121)));

			if($ret['ok'] == 1)
			{
				$data['goods_img'] = $ret['images'][0];
				$data['thumb_img'] = $ret['images'][1];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}

 	}

 	 //type_id 找出属性，根据属性的input_value_type 显示不同的文本域
 	public function getAttrStyle($type_id)
 	{	
 			$where =array();
 			$tid = I('get.type_id');
 			if($tid)
 			{
 				$where['type_id'] =array('eq',$tid);
 			}
 			
 		$attrs = D('attribute')->where($where)->select();

 		$res = "<table width='100%' id='attrTable'>";
		$res .= "<tbody>";
		foreach ($attrs as $attr){
			$res .= "<tr>";
			$res .= "<td class='label'>{$attr['attr_name']}</td>";
			$res .= "<td>";
			$res .= "<input type='hidden' name='attr_id_list[]' value='".$attr['attr_id']."'>";
			//根据attr_input_type不同的值，生成不同的表单元素
			switch ($attr['attr_input_type']){
				case 0: #文本框
					$res .= "<input name='attr_value_list[]' type='text' size='40'>";
					break;
				case 1: #下拉列表
					$res .= "<select name='attr_value_list[]'>";
					$res .= "<option value=''>请选择...</option>";
					$opts = explode(PHP_EOL, $attr['attr_value']);
					foreach ($opts as $opt){
						$res .= "<option value='$opt'>$opt</option>";
					}
					$res .= "</select>";
					break;
				case 2: #多行文本
					$res .= "<textarea name='attr_value_list[]'></textarea>";
					break;
			}
			$res .= "<input type='hidden' name='attr_price_list[]' value='0'>";
			$res .= "</td>";
			$res .= "</tr>";
		}
		$res .= "</tbody>";
		$res .= "</table>";
		return $res;


 	}

 }
 ?>